package com.long1008.controller.statistics;

import com.long1008.pojo.*;
import com.long1008.pojo.*;
import com.long1008.common.poi.PoiUtils;
import com.long1008.service.EmpService;
import com.long1008.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 人事记录模块
 *
 */
@RestController
@RequestMapping("/statistics/record")
public class PerStatisticsController {

    @Autowired
    PersonnelService personnelService;

    @Resource
    EmpService empService;

    @RequestMapping(value = "/personnel", method = RequestMethod.GET)
    public RespPageEntity getAllPer() {
        RespPageEntity pageEntity = new RespPageEntity();
        List<Statistics> statisticsList = new LinkedList<>();

        if (personnelService.getAllEmpEc() != null) {
            List<EmpEc> empEcs = new ArrayList<>();
            empEcs = personnelService.getAllEmpEc();
            for (EmpEc empEc : empEcs) {
                Statistics statistics = new Statistics();
                statistics.setType("奖惩");
                statistics.setEid(empEc.getEid());
                Employee employee = empService.getEmpById(empEc.getEid());
                statistics.setEmpName(employee.getName());
                statistics.setRemark(empEc.getRemark());
                statistics.settDate(empEc.getEcDate());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }

        if (personnelService.getAllAdjustSalary() != null) {
            List<AdjustSalary> adjustSalaries = new ArrayList<>();
            adjustSalaries = personnelService.getAllAdjustSalary();
            for (AdjustSalary adjustSalary : adjustSalaries) {
                Statistics statistics = new Statistics();
                statistics.setType("调薪");
                statistics.setEid(adjustSalary.getEid());
                Employee employee = empService.getEmpById(adjustSalary.getEid());
                statistics.setEmpName(employee.getName());
                statistics.settDate(adjustSalary.getAsDate());
                statistics.setRemark(adjustSalary.getRemark());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }

        if (personnelService.getAllEmpMove() != null) {
            List<EmpMove> empMoves = new ArrayList<>();
            empMoves = personnelService.getAllEmpMove();
            for (EmpMove empMove : empMoves) {
                Statistics statistics = new Statistics();
                statistics.setType("调动");
                statistics.setEid(empMove.getEid());
                Employee employee = empService.getEmpById(empMove.getEid());
                statistics.setEmpName(employee.getName());
                statistics.setRemark(empMove.getRemark());
                statistics.settDate(empMove.getRemoveDate());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }

        if (personnelService.getAllEmpTrains() != null) {
            List<EmpTrain> empTrains = new ArrayList<>();
            empTrains = personnelService.getAllEmpTrains();
            for (EmpTrain empTrain : empTrains) {
                Statistics statistics = new Statistics();
                statistics.setType("培训");
                statistics.settDate(empTrain.getTrainDate());
                statistics.setRemark(empTrain.getRemark());
                statistics.setEid(empTrain.getEid());
                Employee employee = empService.getEmpById(empTrain.getEid());
                statistics.setEmpName(employee.getName());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }
        if (personnelService.getAllEmpTrains() != null) {
            List<EmpRetire> empRetires = new ArrayList<>();
            empRetires = personnelService.getAllEmpRetires();
            for (EmpRetire empRetire : empRetires) {
                Statistics statistics = new Statistics();
                statistics.setType("离职");
                statistics.settDate(empRetire.getRetireDate());
                statistics.setRemark(empRetire.getRetireReason());
                statistics.setEid(empRetire.getEid());
                statistics.setEmpName(empRetire.getEmpName());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }
        // 使用匿名比较器排序
        statisticsList.sort((s1, s2) -> {
            if (s1.gettDate().before(s2.gettDate())) {
                return -1;
            } else if (s1.gettDate().after(s2.gettDate())) {
                return 1;
            } else {
                return 0;
            }
        });
        pageEntity.setData(statisticsList);
        Long totalCount = (long) statisticsList.size();
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportRecordExcel() {
        List<Statistics> statisticsList = new LinkedList<>();
        if (personnelService.getAllEmpEc() != null) {
            List<EmpEc> empEcs = new ArrayList<>();
            empEcs = personnelService.getAllEmpEc();
            for (EmpEc empEc : empEcs) {
                Statistics statistics = new Statistics();
                statistics.setType("奖惩");
                statistics.setEid(empEc.getEid());
                statistics.setRemark(empEc.getRemark());
                statistics.settDate(empEc.getEcDate());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }

        if (personnelService.getAllAdjustSalary() != null) {
            List<AdjustSalary> adjustSalaries = new ArrayList<>();
            adjustSalaries = personnelService.getAllAdjustSalary();
            for (AdjustSalary adjustSalary : adjustSalaries) {
                Statistics statistics = new Statistics();
                statistics.setType("调薪");
                statistics.setEid(adjustSalary.getEid());
                statistics.settDate(adjustSalary.getAsDate());
                statistics.setRemark(adjustSalary.getRemark());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }

        if (personnelService.getAllEmpMove() != null) {
            List<EmpMove> empMoves = new ArrayList<>();
            empMoves = personnelService.getAllEmpMove();
            for (EmpMove empMove : empMoves) {
                Statistics statistics = new Statistics();
                statistics.setType("调动");
                statistics.setEid(empMove.getEid());
                statistics.setRemark(empMove.getRemark());
                statistics.settDate(empMove.getRemoveDate());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }

        if (personnelService.getAllEmpTrains() != null) {
            List<EmpTrain> empTrains = new ArrayList<>();
            empTrains = personnelService.getAllEmpTrains();
            for (EmpTrain empTrain : empTrains) {
                Statistics statistics = new Statistics();
                statistics.setType("培训");
                statistics.settDate(empTrain.getTrainDate());
                statistics.setRemark(empTrain.getRemark());
                statistics.setEid(empTrain.getEid());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }
        if (personnelService.getAllEmpTrains() != null) {
            List<EmpRetire> empRetires = new ArrayList<>();
            empRetires = personnelService.getAllEmpRetires();
            for (EmpRetire empRetire : empRetires) {
                Statistics statistics = new Statistics();
                statistics.setType("离职");
                statistics.settDate(empRetire.getRetireDate());
                statistics.setRemark(empRetire.getRetireReason());
                statistics.setEid(empRetire.getEid());
                statistics.setWorkID(String.format("%08d", statistics.getEid()));
                statisticsList.add(statistics);
            }
        }
        // 使用匿名比较器排序
        statisticsList.sort((s1, s2) -> {
            if (s1.gettDate().before(s2.gettDate())) {
                return -1;
            } else if (s1.gettDate().after(s2.gettDate())) {
                return 1;
            } else {
                return 0;
            }
        });
        return PoiUtils.exportRecordExcel(statisticsList);
    }
}
